package com.zl.learning.db.sqlite.sqlite;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class MyTest {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
    private ExecutorService executorService = Executors.newFixedThreadPool(10);
    private static BlockingQueue<String> queue = new LinkedBlockingQueue<>();

    public static void main(String[] args) {
        new MyTest().start();
    }

    public void start() {
//        new Thread(new T1()).start();
//        new Thread(new T2()).start();
//        new Thread(new T4()).start();
//        new Thread(new T3()).start();
        new Thread(new T5()).start();
    }

    private class T1 implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(5);
                for (int i = 0; i < 10000; i++) {
                    String sql = "SELECT * FROM COMPANY";
                    executorService.execute(() -> {
                        List<Map<String, Object>> res = null;
                        try {
                            res = queryRunner.query(sql, new MapListHandler());
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        System.out.println(res);
                    });
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class T2 implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(5);
                for (int i = 0; i < 10000; i++) {
                    String sql = "INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ('Allen', 25, 'Texas', 15000.00 )";
                    executorService.execute(() -> {
                        queue.add(sql);
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class T3 implements Runnable {

        @Override
        public void run() {
            try {
                for (int i = 0; i < 10000; i++) {
                    String sql = "INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ('Allen', 25, 'Texas', 15000.00 )";
                    executorService.execute(() -> {
                        try {
                            int res = queryRunner.update(sql);
                            System.out.println(res);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    });
                    Thread.sleep(3);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class T4 implements Runnable{

        @Override
        public void run() {
            String sql = "";
            try {
                while ((sql = queue.take()) != null) {
                    try {
                        int res = queryRunner.update(sql);
                        System.out.println(res);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class T5 implements Runnable{

        @Override
        public void run() {
            String sql = "select * from COMPANY";
            List<Map<String, Object>> res = null;
            try {
                res = queryRunner.query(sql, new MapListHandler());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if(res !=null) res.forEach(System.out::println);
        }
    }
}
